import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

#打开浏览器->输入网址
#打开浏览器驱动管理
# ChromeIns=ChromeDriverManager().install()
#
# #创建谷歌浏览器驱动对象
# driver=webdriver.Chrome(service=Service(ChromeIns))
#
# time.sleep(2)
# #输入网址
# driver.get("https://www.baidu.com")
#
# time.sleep(2)
# #输入搜索
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("周杰伦")
#
# time.sleep(2)
# #点击百度一下按钮
# driver.find_element(By.CSS_SELECTOR,"#su").click()
#
# time.sleep(5)
# #关闭浏览器
# driver.quit()

#打开浏览器驱动管理
# ChromeIns=ChromeDriverManager().install()
# #
# # #创建谷歌浏览器驱动对象
# driver=webdriver.Chrome(service=Service(ChromeIns))
#
# driver.get("https://www.baidu.com")
# # 查找多个元素：find_elements
# ret=driver.find_elements(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li")
#
# for i in ret:
#     print(i.text)
#
# driver.quit()


ChromeIns=ChromeDriverManager().install()
driver=webdriver.Chrome(service=Service(ChromeIns))

driver.get("https://www.baidu.com")

print("befor: "+ driver.title)
print("befor: "+ driver.current_url)

driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(6)").click()

#获取当前句柄   切换标签页
curHandle=driver.current_window_handle
allHandle=driver.window_handles #获取所有句柄

for handle in allHandle:
    if handle!=allHandle:
        driver.switch_to.window(handle)#切换句柄


print("after: "+ driver.title)
print("after: "+ driver.current_url)
time.sleep(3)


driver.quit()















